package org.yuxiong.utils;

import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.regex.Pattern;

public class SuperDate {
     private Date myDate;
     private static final String patternDate="yyyy-MM-dd";
     private static final String patternYearMonth="yyyy-MM";
     private static final String patternDateTime="yyyy-MM-dd HH:mm:ss";
     private static final String patternDateMinutes="yyyy-MM-dd HH:mm";
     private static final String patternDateTimeString="yyyyMMddHHmmss";
     private SimpleDateFormat sdf;
     private GregorianCalendar cal;
     
     /**
      * 初始化日�?
      */
     private void initialize(){
    	 cal=new GregorianCalendar();
    	 cal.setTime(this.myDate);
     }
     /**
      *�?��列构造方�?
      */
     public SuperDate(){
    	 this.myDate=new Date();
    	 initialize();
     }
     public SuperDate(long dlong){
    	 this.myDate=new Date();
    	 this.myDate.setTime(dlong);
    	 initialize();
     }
     public SuperDate(Date ddate){
    	 this.myDate=ddate;   	 
    	 initialize();
     }
     
     /*public MySuperDate(String dateTimeString){
    	 //this(dateTimeString, true);
    	 
     }*/

	public SuperDate(String dateTimeString, boolean isDateTime){
    	 if(dateTimeString==null|| dateTimeString.length()==0){
    		 this.myDate=new Date();
    	 }else{
    		 if(isDateTime) sdf=new SimpleDateFormat(patternDateTime);
    		 else sdf=new SimpleDateFormat(patternDate);
    		 this.myDate=sdf.parse(dateTimeString, new ParsePosition(0));
    	 }
    	 initialize();
     }
	 public SuperDate(String dateMinutes){
		 if(dateMinutes ==null || dateMinutes.length() == 0){
			 this.myDate=new Date();
		 }else{
			 sdf=new SimpleDateFormat(patternDateMinutes); 
			 try {
				this.myDate=sdf.parse(dateMinutes);
			} catch (ParseException e) {			
				e.printStackTrace();
			}
		 }
		 initialize();		
	 }
     public SuperDate(String Year,String Month,String Day){
    	 sdf=new SimpleDateFormat(patternDate);
    	 this.myDate=sdf.parse(Year+"-"+Month+"-"+Day,new ParsePosition(0));
    	 initialize();
     }
     public SuperDate(int Year,int Month,int Day){
    	 sdf=new SimpleDateFormat(patternDate);
    	 this.myDate=sdf.parse(Integer.toString(Year)+"-"+Integer.toString(Month)+"-"+
    			 Integer.toString(Day),new ParsePosition(0));
    	 initialize();
     }
     public SuperDate(String Year,String Month,String Day,String Hour,String Minute,String Second){
    	 sdf=new SimpleDateFormat(patternDateTime);
    	 this.myDate=sdf.parse(Year+"-"+Month+"-"+Day+"  "+Hour+":"+Month+":"+Second,new ParsePosition(0));
    	 initialize();
     }
     public SuperDate(int Year,int Month,int Day,int Hour,int Minute,int Second){
    	 sdf=new SimpleDateFormat(patternDate);
    	 this.myDate=sdf.parse(Integer.toString(Year)+"-"+Integer.toString(Month)+"-"+
    			 Integer.toString(Day)+" "+Integer.toString(Hour)+":"+Integer.toString(Minute)
    			 +":"+Integer.toString(Second),new ParsePosition(0));
    	 initialize();
     }
     /**
      * get方法
      * @return
      */
     public int getYear(){
    	 return cal.get(Calendar.YEAR);
     }
     public int getMonth(){
    	 return cal.get(Calendar.MONTH)+1;
     }
     public int getDay(){
    	 return cal.get(Calendar.DAY_OF_MONTH);
     }
     public int getHour(){
    	 return cal.get(Calendar.HOUR_OF_DAY);
     }
     public int getMinute(){
    	 return cal.get(Calendar.MINUTE);
     }
     public int getSecond(){
    	 return cal.get(Calendar.SECOND);
     }
     public long getMilliSecond(){
    	 return cal.getTime().getTime();
     }
     public boolean after(SuperDate when){
    	 return this.myDate.after(when.myDate);
     }
     public boolean before(SuperDate when){
    	 return this.myDate.before(when.myDate);
     }
     public int compareTo(SuperDate when){
    	 return this.myDate.compareTo(when.myDate);
     }
     public String getDateString(){
    	 sdf=new SimpleDateFormat(patternDate);
    	 return (sdf.format(this.myDate));
     }
     public String getDateTimeString(){
    	 sdf=new SimpleDateFormat(patternDateTime);
    	 return (sdf.format(this.myDate));
     }
     public String getDateTimeStrString(){
    	 sdf=new SimpleDateFormat(patternDateTimeString);
    	 return (sdf.format(this.myDate));
     }
     public String getYearMonthString(){
    	 sdf=new SimpleDateFormat(patternYearMonth);
    	 return (sdf.format(this.myDate));
     }
     public static String formatDateTime(Date d,String pattern){
    	 SimpleDateFormat sdf2=new SimpleDateFormat(pattern,Locale.CANADA);
    	 return  (sdf2.format(d));
     }
     public String getFormatDateTime(String pattern){
    	 return new SimpleDateFormat(pattern).format(myDate);
     }
     public Date getDate(){
    	 return this.myDate;
     }
     public Calendar getCalendar(){
    	 return this.cal;
     }
     public void add(int field,int amount){
    	this.cal.add(field, amount);
    	this.myDate=cal.getTime();
     }
     public SuperDate getAddedDate(int field,int amount){
    	 SuperDate addedSuperDate=new SuperDate(this.myDate);
    	 addedSuperDate.add(field, amount);
    	 return addedSuperDate;
     }
     public static SuperDate getFirstDayInMonthDate(SuperDate thesdt){
    	 return new SuperDate(thesdt.getYear()+"-"+thesdt.getMonth()+"-01 00:00:00");
     }
     public static boolean isToday(SuperDate sdf3,SuperDate sdf4){
    	 return !sdf3.before(new SuperDate(sdf4.getDateString()+"00:00:00"))&&
    	        !sdf3.after(new SuperDate(sdf4.getDateString()+"23:59:59"));
     }
     public static long getTimeInteval(SuperDate sdf5,SuperDate sdf6,int intUnit){
    	 long intevalms;
    	 long returnValue=0;
    	 intevalms=sdf6.myDate.getTime()-sdf5.myDate.getTime();
    	 switch(intUnit){
    	      case Calendar.DAY_OF_MONTH:
    	            returnValue=intevalms/(1000*3600*24);
    	            break;
    	      case Calendar.DAY_OF_YEAR:
	            returnValue=intevalms/(1000*3600*24);
	            break;   
    	      case Calendar.HOUR:
    	    	  returnValue=intevalms/(1000*3600);
    	      case Calendar.MINUTE:
    	    	  returnValue=intevalms/(1000*60); 
    	      case Calendar.SECOND:
    	    	  returnValue=intevalms/1000; 
    	 }
    	 return (returnValue);
     }
     /**
      * 取得这个月的�?���?
      */
     public int getActualMaximumDaysInMonth(){
    	 return getCalendar().getActualMaximum(Calendar.DAY_OF_MONTH);
     }
     public static String getPatternDateMinutes() {
 		return patternDateMinutes;
 	}
     /**
      * 取得这个月的第一个星期的第一�?
      */
     public int getActualFirstWeekDayInMonth(){
    	 Calendar calendar=new SuperDate(getDate()).getCalendar();
    	 calendar.set(Calendar.DAY_OF_MONTH, 1);
    	 return calendar.get(Calendar.DAY_OF_WEEK);
     }
     public String getPrevSunday(SuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, -1*(7+week-1)).getDateString();
     }
     public String getNextSunday(SuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, (7+week-1)).getDateString();
     }
     public String getNextThirdSunday(SuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, (14-week+1)).getDateString();
     }
     public String getCurrSunday(SuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, (1-week)).getDateString(); 
     }
     public String getCurrSaturDay(SuperDate sDate){
    	 int week=sDate.getCalendar().get(Calendar.DAY_OF_WEEK);
    	 return sDate.getAddedDate(Calendar.DAY_OF_YEAR, (7-week)).getDateString();
     }
     public Date getTodayStartTime(){
    	 this.cal.set(this.getYear(), this.getMonth()-1,this.getDay(),0,0,0);
    	 return this.cal.getTime();
     }
     public Date getTodayNextTime(){
    	 this.cal.set(this.getYear(), this.getMonth()-1,this.getDay(),0,0,0);
    	 this.cal.add(GregorianCalendar.DAY_OF_YEAR, 1);
    	 return this.cal.getTime();
     }
     public String getEventDateString(){
    	 String strYear=String.valueOf(getYear()).substring(2);
    	 String strMonth=String.valueOf(getMonth());
    	 if(getMonth()<10)strMonth="0"+strMonth;
    	 String strDay=String.valueOf(getDay());
    	 if(getDay()<10)strDay="0"+strDay;
    	 return strYear+"."+strMonth+"."+strDay;
     }
     
     public static SuperDate getSuperDateNotNull(Date ld,String def_date){
    	 SuperDate sdtemp=null;
    	 try{
    	       sdtemp=new SuperDate(ld);
    	 }catch(Exception e){
    		 if(!isDateString(def_date)) sdtemp=new SuperDate();
    		 else sdtemp=new SuperDate(def_date);
    	 }
    	 return sdtemp;
     }
     public static boolean isDateTimeString(String s) {
 		
 		return match("(\\d{4})-(\\d{2})-(\\d{2}) (\\d{2}):(\\d{2}):(\\d{2})", s);
 	}
      
	public  static boolean isDateString(String s) {
		
		return match("(\\d{4})-(\\d{2})-(\\d{2})", s);
	}
	public static boolean match(String regex,String str){
		return Pattern.matches(regex, str);
	}
	public int getDiscrepancyDay(){
		long dis=new Date().getTime()-this.myDate.getTime();
		return (int )(dis/(24*60*60*1000));
	}
	public static int getAge(Date birthday){
		return new SuperDate().getYear()-new SuperDate(birthday).getYear();
	}
	public static void main(String[] args){
		SuperDate sdt=new SuperDate("1988-02-15 11:11:11");
		System.out.println(SuperDate.getAge(sdt.getDate()));
		SuperDate sdt2=new SuperDate("1988-02-15 11:11:11");
		SuperDate sdt3=getFirstDayInMonthDate(sdt2);
		System.out.println(sdt3);
		Date d=new SuperDate().getTodayNextTime();
		SuperDate sdt4=new SuperDate(d);		
		System.out.println(sdt4.getDateTimeString());
	}
	
     
     
     
     
     
}
